package cn.wolfcode.Controller;


import cn.wolfcode.domain.Business;
import cn.wolfcode.meiju.BussinessReportEnum;
import cn.wolfcode.qo.BussinessReportObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.util.RequirePermission;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/BussinessReport")
public class BusinessReportController {

    @Autowired
    private IConsumptionService consumptionService;
    @Autowired
    private IBusinessService businessService;


    /**
     *根据报表的参数，（分组类别（根据门店,年月日进行），总订单数，总结算金额，总实惠金额，总实收金额）
     * 高级查询：（选择门店，时间段查询，结算单状态，预约类型）
     * 根据这些要显示的字段，我们可以通过结算单和门店单进行连表
     *
     * 首先根据页面的额参数，写分组sql
     * 因为分组有四个，门店，年，月，日，这样的话我们需要写4条sql语句
     * 我们可以通过${}传入动态的分组依据
     *
     * 在分组依据数据回显时，我们可以将数据信息存入到枚举类，这样修改方便且见名字知道意思
     *
     *
     * 之后加上高级查询
     */
    @RequirePermission(name = "门店报表页面",experssion = "BussinessReport:list")
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") BussinessReportObject QueryObject){

        //回显所有的分组依据
        BussinessReportEnum[] enums = BussinessReportEnum.values();
        model.addAttribute("enums",enums);
        //获取分页数据
        PageInfo<HashMap> pageInfo = consumptionService.selectByBussiness(QueryObject);
        model.addAttribute("pageInfo",pageInfo);
        //将高级查询的门店全部显示
        List<Business> businesses = businessService.listAll();
        model.addAttribute("businesses",businesses);
        //关于数据的回显
        return "businessReport/list";
    }


    @RequirePermission(name = "柱状图页面",experssion = "BussinessReport:listBar")
    @RequestMapping("/listBar")
    public String listBar(Model model,@ModelAttribute("qo") BussinessReportObject queryObject){
        //由于echart不支持集合，需要按照原来的格式
        //[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
        //因此需要将我们获取到的集合转为这样的格式
        //首先获取所有数据（不要分页）
        queryObject.setPageSize(0);
        List<HashMap> list = consumptionService.selectByBussiness(queryObject).getList();
        //使用四个集合来存储对应的数据
        List<Object> totalAmountList = new ArrayList<>();
        List<Object> groupTypeList = new ArrayList<>();
        List<Object> countList = new ArrayList<>();
        List<Object> disAmountList = new ArrayList<>();
        List<Object> payAmountList = new ArrayList<>();
        //遍历数据源给集合赋值
        list.forEach(item->{
            countList.add(item.get("number"));
            totalAmountList.add(item.get("total_amount"));
            disAmountList.add(item.get("discount_amount"));
            payAmountList.add(item.get("pay_amount"));
            groupTypeList.add(item.get("groupType"));
        });
        System.out.println(countList);
        model.addAttribute("number", JSON.toJSONString(countList));
        model.addAttribute("total_amount",JSON.toJSONString(totalAmountList));
        model.addAttribute("disAmountList",JSON.toJSONString(disAmountList));
        model.addAttribute("pay_amount",JSON.toJSONString(payAmountList));
        model.addAttribute("groupType",JSON.toJSONString(groupTypeList));

        System.out.println(queryObject.getGroupType());
        String groupType = BussinessReportEnum.getStatusByValue(queryObject.getGroupType());
        model.addAttribute("a",groupType);

        return "businessReport/bar2";
    }


}
